package sample.command
{
	import org.flest.command.ICommand;
	import org.flest.model.ModelLocator;
	import sample.data.User;

	public class SubmitCmd implements ICommand
	{
		/**
		 * Flest command can have any number of parameters and 
		 * return back any desirable object reference. The command
		 * is a closure, it will run in FlestController context. 
		 * FlestController has a reference to the application itself 
		 * named 'app', therefore it is safe and legal to call the 
		 * application from the command like this: 
		 * 
		 *         this.getApp().currentState = "newState";
		 * 
		 * A Command may contain as many methods/functions as required.
		 */		
		public var submitCmd:Function = function(newName:String, newSurname:String):void
		{
			var user:User = new User();
			user.name = newName;
			user.surname = newSurname;
			/**
			 * ModelLocator is a Singleton, it provides an access to 
			 * an enhanced associative array that keeps key-value 
			 * references to all application data objects. This is 
			 * a single repository of all data objects.
			 */
			ModelLocator.getInstance().addValue("user", user);
			
			/**
			 * Switch to new view State
			 */
			this.getApp().currentState = "helloState";
		}
		
		public var backCmd:Function = function():void
		{
			/**
			 * Clean up the data repository.
			 */
			ModelLocator.getInstance().removeValue("user");
			
			/**
			 * Switch back to original view State.
			 */
			this.getApp().currentState = "";
		}
	}
}